package simpleftp.client.control;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import simpleftp.client.gui.FTPClientWindow;

public class CommandDispatcher implements Runnable 
{
	public static final String NL = "\n";
	private static final String USER = "USER";
	private static final String PASS = "PASS";
	private static final String SERVER = "SERVER";
	private static final String PORT = "PORT";
	private static final String SETTINGS_ROOT = "simpleftpsettings";
	private static final String DEFAULT_SETTINGS = SETTINGS_ROOT + "/default";
	private static final String LAST_SETTINGS = SETTINGS_ROOT + "/last";
	private static final String DEF_USER = "paris";
	private static final String DEF_PASS = "paris";
	private static final String DEF_SERVER = "127.0.0.1";
	private static final String DEF_PORT = "7802";
	private final ExecutorService executorPool;
	private final CompletionService<Boolean> completionPool;
	private final FTPClientWindow window;
	private final int numberOfCores;
	private final double blockingCoefficient = 0.9;
	private final int poolSize;
	private BlockingQueue<Command> commands;
	private SimpleFTP client;
	private Preferences userPrefs;
	private String server;
	private String port;
	private String user;
	private String pass;
	private boolean alreadyConnected;

	public CommandDispatcher(BlockingQueue<Command> commandQueue, FTPClientWindow window) 
	{
		this.commands = commandQueue;
		this.window = window;
		alreadyConnected = false;
		numberOfCores = Runtime.getRuntime().availableProcessors();
		poolSize = (int) (numberOfCores / (1 - blockingCoefficient));
		executorPool = Executors.newFixedThreadPool(poolSize);
		completionPool = new ExecutorCompletionService<Boolean>(executorPool);
		client = new SimpleFTP();
	}

	public void run() {
		try {
			loadStartupPreferences();
			Command cmd = this.commands.take();
			while (!cmd.equals(Command.QUIT)) {
				switch (cmd) {
				case CONNECT:
					connect();
					break;
				case DISCONNECT:
					disconnect();
					break;
				case STOR:
					upload();
					break;
				case LIST:
					listDir();
					break;
				case RETR:
					download();
					break;
				case PWD:
					pwd();
					break;
				case CLEAR:
					clearConsole();
					break;
				case LOADPREFS:
					loadPreferences();
					break;
				case SAVEPREFS:
					savePreferences();
					break;
				case DELPREF:
					deletePreference();
					break;
				case CWD:
					cwd();
					break;
				case GOBACK:
					goBack();
					break;
				default:
					break;
				}
				cmd = this.commands.take();
			}
			saveLastPreferences();
			disconnect();
			executorPool.shutdown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void clearConsole() {
		window.console.setText("");
	}

	private void saveLastPreferences() 
	{
		userPrefs = Preferences.userRoot().node(LAST_SETTINGS);
		userPrefs.put(USER, window.tfusername.getText());
		userPrefs.put(PASS, new String(window.pfuserpass.getPassword()));
		userPrefs.put(SERVER, window.tfservername.getText());
		userPrefs.put(PORT, window.tfport.getText());
		try {
			userPrefs.flush();
		} catch (BackingStoreException e) {
			e.printStackTrace();
		}
	}

	private void loadStartupPreferences() 
	{
		try {
			userPrefs = Preferences.userRoot();
			if (!userPrefs.nodeExists(DEFAULT_SETTINGS)) {
				userPrefs = Preferences.userRoot().node(DEFAULT_SETTINGS);
				userPrefs.put(USER, DEF_USER);
				userPrefs.put(PASS, DEF_PASS);
				userPrefs.put(SERVER, DEF_SERVER);
				userPrefs.put(PORT, DEF_PORT);
				userPrefs.flush();
			}
			if (userPrefs.nodeExists(LAST_SETTINGS)) {
				userPrefs = Preferences.userRoot().node(LAST_SETTINGS);
			} 
			window.tfusername.setText(userPrefs.get(USER, DEF_USER));
			window.pfuserpass.setText(userPrefs.get(PASS, DEF_PASS));
			window.tfservername.setText(userPrefs.get(SERVER, DEF_SERVER));
			window.tfport.setText(userPrefs.get(PORT, DEF_PORT));
		} 
		catch (BackingStoreException e) 
		{
			e.printStackTrace();
		}
	}
	
	private void loadPreferences() {
		try {
			userPrefs = Preferences.userRoot().node(SETTINGS_ROOT);
			String[] userSavedPrefs = userPrefs.childrenNames();
			String s = (String) JOptionPane.showInputDialog(window,
					"Choose preference to load:", "Load Preferences",
					JOptionPane.QUESTION_MESSAGE, null, userSavedPrefs, userSavedPrefs[0]);
			if ((s != null) && (s.length() > 0)) {
				userPrefs = Preferences.userRoot().node(SETTINGS_ROOT + "/" + s);
				window.tfusername.setText(userPrefs.get(USER, DEF_USER));
				window.pfuserpass.setText(userPrefs.get(PASS, DEF_PASS));
				window.tfservername.setText(userPrefs.get(SERVER, DEF_SERVER));
				window.tfport.setText(userPrefs.get(PORT, DEF_PORT));
				
				window.console.append(NL + "User preferences " + s + " loaded.");
			} else {
				window.console.append(NL + "Preferences loading operation cancelled.");
			}
		} catch (BackingStoreException e) {
			e.printStackTrace();
		}
	}

	private void savePreferences() {
		String s = (String) JOptionPane.showInputDialog(window,
				"Save the current connection info as:", "Save Preferences",
				JOptionPane.QUESTION_MESSAGE, null, null, null);
		if ((s != null) && (s.length() > 0)) {
			userPrefs = Preferences.userRoot().node(SETTINGS_ROOT + "/" + s);
			userPrefs.put(USER, window.tfusername.getText());
			userPrefs.put(PASS, new String(window.pfuserpass.getPassword()));
			userPrefs.put(SERVER, window.tfservername.getText());
			userPrefs.put(PORT, window.tfport.getText());
			try {
				userPrefs.flush();
			} catch (BackingStoreException e) {
				window.console.append(NL + "Could not save preferences: "
						+ e.getMessage());
				e.printStackTrace();
			}
			window.console.append(NL + "Preferences saved as " + s);
		} else {
			window.console.append(NL + "Preferences saving operation cancelled.");
		}
	}
	
	private void deletePreference() {
		try {
			userPrefs = Preferences.userRoot().node(SETTINGS_ROOT);
			String[] userSavedPrefs = userPrefs.childrenNames();
			String s = (String) JOptionPane.showInputDialog(window,
					"Choose preference to delete:", "Delete Preference",
					JOptionPane.QUESTION_MESSAGE, null, userSavedPrefs, userSavedPrefs[0]);
			if ((s != null) && (s.length() > 0)) {
				userPrefs = Preferences.userRoot().node(SETTINGS_ROOT + "/" + s);
				userPrefs.removeNode();
				userPrefs.flush();
				window.console.append(NL + "User preferences " + s + " deleted.");
			} else {
				window.console.append(NL + "Preferences deletion operation cancelled.");
			}
		} catch (BackingStoreException e) {
			e.printStackTrace();
		}
	}
	
	private void download() 
	{
		if (!alreadyConnected) {
			window.console.append(NL + "You are not connected to any server.");
			return;
		}
		
		TreePath[] paths = window.tree.getSelectionPaths();
		
		if(paths != null){
			
			File dir = new File(FileSystemView.getFileSystemView().getHomeDirectory() + "/" + "simpleFTPDownloads");
			dir.mkdirs();
			
			List<String> filePaths = new ArrayList<String>();
			
			for(TreePath p : paths){
				if(isItDirectory(p) == false){
					filePaths.add(getPath(p));
				}
				else {
					window.console.append("\nDownload cancelled for " + getPath(p) + ", you can't download a directory\n");
					System.out.println("\nDownload cancelled for " + getPath(p) + ", you can't download a directory\n");
				}
			}
			
			if(filePaths.isEmpty() != false){
				List<Future<Boolean>> downloaders = new ArrayList<Future<Boolean>>(filePaths.size());
				for (String path : filePaths) 
				{
					Downloader downloader = new Downloader (path, window, server, port, user, pass);
					downloaders.add(completionPool.submit(downloader));
				}
			}
		}
		else 
		{
			window.console.append(NL + "Download action cancelled, no file selected.");
		}
	}
	
	private String getPath(TreePath path){
		
		Object[] elements = (Object[]) path.getPath();
		StringBuilder sb = new StringBuilder();
		
		sb.append(FileSystemView.getFileSystemView().getHomeDirectory() + "/" + "simpleFTPDownloads" + "/" 
				+ elements[elements.length-1].toString() + "   ");
		
		for (int i = 2; i < elements.length - 1; i++){
			sb.append(elements[i].toString() + "/");
		}
		sb.append(elements[elements.length-1].toString());
		
		String s = new String(sb);
		
		System.out.println("File path : " + s);
		
		return s;
	}
	
	private boolean isItDirectory(TreePath path){
		
		DefaultMutableTreeNode t = (DefaultMutableTreeNode) path.getLastPathComponent();
		
		if(t.isLeaf() == true) return false;
		else return true;
	
	}


	private void upload() throws ClassNotFoundException 
	{
		try {
			if (!alreadyConnected) {
				window.console.append(NL + "You are not connected to any server.");
				return;
			}
			final JFileChooser fc = new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
			fc.setMultiSelectionEnabled(true);
			fc.setDragEnabled(true);
			int returnVal = fc.showDialog(window, "Upload");
			if (returnVal == JFileChooser.APPROVE_OPTION) 
			{
				
				File[] files = fc.getSelectedFiles();
				List<Future<Boolean>> uploaders = new ArrayList<Future<Boolean>>(files.length);
				for (File file : files) 
				{
					Uploader uploader;
						uploader = new Uploader (file, window, server, port, user, pass, client.getCurrentDir());
					uploaders.add(completionPool.submit(uploader));
				}
			}
			else 
			{
				window.console.append(NL + "Upload action cancelled.");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void listDir() throws ClassNotFoundException 
	{
		if (!alreadyConnected) 
		{
			window.console.append(NL + "You are not connected to any server.");
			return;
		}
		try {
			String dirContent = client.ls();
			window.console.append(NL + dirContent);
			// TODO complete / rewrite to get the file tree from the server. - DONE
			window.root.removeAllChildren();
			DefaultMutableTreeNode userRoot = client.getFileTree();
			window.root.add(userRoot);
			window.tree.updateUI();
		} catch (IOException e) {
			window.console.append(NL + e.getMessage());
			e.printStackTrace();
		}
	}
	
	/*private void populateTree(DefaultMutableTreeNode userRoot, File root) {
		// TO BE COMPLETED - WRITE THIS RECURSIVE METHOD - 7 LINES
		File[] files = root.listFiles();
		for(File f : files){
			DefaultMutableTreeNode child = new DefaultMutableTreeNode(f.getName());
			userRoot.add(child);
			if(f.isDirectory()){
				populateTree(child, f);
			}
		}
	}*/

	private void pwd() {
		try {
			if (!alreadyConnected) {
				window.console.append(NL + "You are not connected to any server.");
				return;
			}
			String curDir = client.pwd();
			window.console.append(NL + "Current directory on FTP server: " + curDir);
		} catch (IOException e) {
			window.console.append(NL + e.getMessage());
			e.printStackTrace();
		}
	}

	private void disconnect() {
		try {
			if (alreadyConnected) {
				client.disconnect();
				alreadyConnected = false;
				window.console.append(NL + "Now disconnected");
			} else {
				window.console.append(NL + "Already disconnected");
			}
		} catch (IOException e) {
			window.console.append(NL + e.getMessage());
			e.printStackTrace();
		}

	}

	private void connect() {
		if (alreadyConnected) {
			window.console.append(NL
					+ "You are already connected to this server.");
			return;
		}
		server = window.tfservername.getText();
		if (server == null || "".equals(server)) {
			window.console.append(NL
					+ "You must specify the server IP address.");
			return;
		}
		port = window.tfport.getText();
		if (port == null || "".equals(port)) {
			window.console.append(NL
					+ "You must specify the server port I must connect to.");
			return;
		}
		user = window.tfusername.getText();
		if (user == null || "".equals(user))
			user = "anonymous";
		pass = new String(window.pfuserpass.getPassword());
		if (pass == null || "".equals(pass))
			pass = "anonymous";
		try {
			client.connect(server, Integer.parseInt(port), user, pass);
			alreadyConnected = true;
			window.console.append(NL + "Now connected");
			pwd();
			listDir();
		} catch (IOException e) {
			window.console.append(NL + e.getMessage());
			e.printStackTrace();
		} catch (NumberFormatException nfe) {
			window.console.append(NL + nfe.getMessage());
			nfe.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void cwd() {
		try {
			if (!alreadyConnected) {
				window.console.append(NL + "You are not connected to any server.");
				return;
			}
			// get the path to the node selected by the user
			final TreePath tp = window.tree.getSelectionPath(); 
			// We store the last component of the path we obtained just before
			final DefaultMutableTreeNode tn = (DefaultMutableTreeNode) tp.getLastPathComponent(); 		
			
			if (tn != null && tn.isLeaf()==false) // if the component exists and is a directory (is not a leaf)
			{
				Object[] elementsOfPath = (Object[]) tp.getPath();
				StringBuilder sb = new StringBuilder();
				
				for (int i = 2; i < elementsOfPath.length - 1; i++){
					sb.append(elementsOfPath[i].toString() + "/");
				}
				sb.append(elementsOfPath[elementsOfPath.length-1].toString());
				
				// store the result of the request sent to the server in a boolean variable
				boolean newDir = client.cwd(new String(sb)); 

				// Simple test to check the result 
				if (newDir) {
	                System.out.println("Successfully changed working directory.");
	            } else {
	                System.out.println("Failed to change working directory.");
	            }

				window.console.append(NL + "Current directory on FTP server: " + tn.toString());
				
				window.root.removeAllChildren(); // we clear the tree by removing all the node's children
				window.root.add(tn); // we create the new tree
				window.tree.updateUI(); // we update the user interface view
				
			}
			else 
			{
				window.console.append(NL + "cwd action cancelled.");
			}
		} catch (IOException e) {
			window.console.append(NL + e.getMessage());
			e.printStackTrace();
		}
	}
	
	private void goBack() throws ClassNotFoundException{
		
		try {
			if (!alreadyConnected) {
				window.console.append(NL + "You are not connected to any server.");
				return;
			}
			

			client.goBack(); // we call the method on the client side

			DefaultMutableTreeNode userRoot = client.getFileTree(); // we store the new tree in a variable

			
			System.out.println("Root infos: " + window.root.getChildAt(0));
			window.root.removeAllChildren(); // we clear the tree by removing all the node's children
			window.root.add(userRoot); // we create the new tree
			window.tree.updateUI(); // we update the user interface view
			
			window.console.append(NL + "Current directory on FTP server: " + userRoot.toString());
			
		} catch (IOException e) {
			window.console.append(NL + e.getMessage());
			e.printStackTrace();
		}
	}
}
